package com.atguigu.springcloud.alibaba.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Optional;

@Slf4j
@RestController
public class DemoController {

    @Autowired
    RestTemplate restTemplate1;

    @Value(value = "${server.port}")
    String port;
    @Value(value = "${spring.cloud.nacos.discovery.metadata.gatewayIp}")
    String gatewayIp;
    @Value(value = "${spring.cloud.nacos.discovery.metadata.gatewayPort}")
    String gatewayPort;
    @Value(value = "${spring.cloud.nacos.discovery.clusterName}")
    String clusterName;

    private String serviceName = "nacos-zengbang";

    @GetMapping(value = "/demo1")
    public String demo1(@RequestParam("serviceName") String serviceName, @RequestParam("port") String port) {
        HttpEntity entity = new HttpEntity<>(null, null);
        ResponseEntity<String> exchange = restTemplate1.exchange("http://" + serviceName + "/answer", HttpMethod.GET, entity, String.class);
        return exchange.getBody();
    }

    @GetMapping(value = "/answer")
    public String result() {
        return String.format("访问成功！这是端口%s，时间是%s", port, new Date().toLocaleString());
    }

    @GetMapping(value = "/getgateway")
    public String getgateway() {
        return String.format("访问成功！配置的gateway的ip：%s，配置的gateway的port：%s,配置的集群: %s，时间是：%s",
                gatewayIp, gatewayPort, clusterName, new Date().toLocaleString());
    }
}
